/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 dialog_save_game.cpp

	$Header: /resource_editor/dialog_save_game.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "dialog_save_game.h"

#include <fstream>
#include "bitmap_group.h"
#include "bitmap_group_cache.h"
#include "bound_handler.h"
#include "button.h"
#include "button_cache.h"
#include "external_string.h"
#include "interface_help.h"
#include "replace_keywords.h"
#include "simple_dialog.h"
#include "standard_fonts.h"
#include "text_edit_window.h"

static t_bitmap_group_cache		k_bitmaps( "dialog.save_game" );
static t_external_string		k_text_save_game( "Save_Game.dialog" );
static t_external_string const	k_text_save( "Save.dialog" );
extern t_button_cache			g_large_button;


static const std::string	k_illegal_file_name_characters( "<>*?.:|/\"\'\\" );
static const int			k_max_file_name_length = 255;


// ------------------------------------------------------------------
// save game dialog
// ------------------------------------------------------------------
t_dialog_save_game::t_dialog_save_game( t_window* parent )
                  : t_dialog_file( k_bitmaps.get(), parent )
{
	t_screen_rect  rect;
	t_screen_point point;
	t_help_block const&		help = get_help_block( "system_menu" );

	// add save button
	point = m_bitmaps->find( "save_location" )->get_rect().top_left() + m_background_origin;
	m_save_button = new t_button( g_large_button.get(), point, m_background, k_text_save );
	m_save_button->set_click_handler( bound_handler( *this, &t_dialog_save_game::save_click ) );
	m_save_button->enable( false );
	set_help ( m_save_button, help, "save_game");

	// create title
	create_title( k_text_save_game );

	// create edit box
	t_text_edit_handler handler;

	rect = m_bitmaps->find( "edit_box" )->get_rect() + m_background_origin;
	m_edit_box = new t_text_edit_window( get_font( rect.height() ), rect, m_background,
		                                 "", t_pixel_24(0,0,0) );
	m_edit_box->set_drop_shadow( true, t_pixel_24(200,200,200));
	handler = bound_handler( *this, &t_dialog_save_game::name_change );
	m_edit_box->set_change_handler( handler );
	handler = bound_handler( *this, &t_dialog_save_game::name_finished );
	m_edit_box->set_end_edit_handler( handler );
	read_directory( "games" );
	m_edit_box->begin_edit();	// start the cursor blinking
}

// ------------------------------------------------------------------
// save game dialog
// ------------------------------------------------------------------
std::string t_dialog_save_game::get_file_name() const
{
	return m_folder + m_edit_box->get_text() + ".h4s";;
}

// ------------------------------------------------------------------
// save game dialog
// ------------------------------------------------------------------
void t_dialog_save_game::select_file( t_file_data& data )
{
	if (!data.is_folder)
	{
		m_edit_box->set_text( data.file_name );
		m_save_button->enable( true );
	}
	else 
	{
		m_edit_box->set_text( "" );
		m_save_button->enable( false );
	}
}

// ------------------------------------------------------------------
// save game dialog
// respond to a change in the edit box
// ------------------------------------------------------------------
void t_dialog_save_game::name_change( t_text_edit_window* )
{
	bool file_name_okay = false;

	std::string text = m_edit_box->get_text();

	if (!text.empty()) {
		if (text.find_first_of( k_illegal_file_name_characters ) == std::string.npos) {
			if (text[0] != ' ' && get_file_name().length() <= k_max_file_name_length) {
				file_name_okay = true;
			}
		} 
	}
	
	m_save_button->enable( file_name_okay );
}

// ------------------------------------------------------------------
// save game dialog
// respond to a change in the edit box
// ------------------------------------------------------------------
void t_dialog_save_game::name_finished( t_text_edit_window* )
{
	finish();
}


// ------------------------------------------------------------------
// save game dialog
// ------------------------------------------------------------------
void t_dialog_save_game::save_click( t_button* )
{
	finish();
}


// ------------------------------------------------------------------
// save game dialog
// ------------------------------------------------------------------
static t_external_string k_text_overwrite( "overwrite.dialog" );
void t_dialog_save_game::finish()
{
	if (!m_save_button->is_enabled()) {
		return;
	}

	std::filebuf   file;
	std::string    file_name = m_edit_box->get_text();
	std::string    full_name = get_file_name();

	if (file.open( full_name.c_str(), std::ios::in | std::ios::binary ) != 0)
	{
		std::string text;

		text = replace_keywords( k_text_overwrite, "%file_name", file_name );
		if (!yes_no_dialog( text ))
			return;
	}

	m_return_value = true;
	close();
}
